package org.wattdepot.cli;

import java.util.List;
import org.wattdepot.client.WattDepotClientException;
import org.wattdepot.resource.source.jaxb.Source;

/**
 * Executes the sources command.
 * 
 * @author Scott Wong and Kelli Sawai
 * 
 */
public class ListSources extends CommandLineInterface implements WattDepotCliCommand {

  /**
   * Prints a table summarizing the public sources in the system, one per line.
   * 
   * @param args The arguments required to execute this method.
   * @return The results of the command.
   */
  public String doCommand(String args) {
    try {
      if (args.compareToIgnoreCase("sources") == 0) {
        return listSources();
      }
      else {
        return "The command you have entered is invalid.";
      }
    }
    catch (WattDepotClientException e) {
      return "An error occured while executing the requested command.";
    }
    catch (Exception e) {
      return "An error occured while executing the requested command.";
    }
  }

  /**
   * Prints a table summarizing the public sources in the system, one per line.
   * 
   * @return The list of current sources.
   * @throws WattDepotClientException If problems occur with the connection to the server
   */
  private String listSources() throws WattDepotClientException {
    try {
      StringBuffer buffer = new StringBuffer();
      String format = "%1$-15s %2$-15s %3$-95s\n";

      // Column headings
      buffer.append(String.format(format, "Name:", "Parent:", "Description:"));
      List<Source> sources = client.getSources();

      // Attach name, parent, and description information
      for (Source source : sources) {
        buffer.append(String.format(format, source.getName(),
            getParent(source.getName()), source.getDescription()));
      }

      return buffer.toString();
    }
    catch (WattDepotClientException e) {
      return "Error: Can't connect to server.";
    }
  }

  /**
   * Gets the parent of the source.
   * 
   * @param source The source
   * @return parent The parent of the source
   */
  private String getParent(String source) {
    String parent = "";
    if (source.compareToIgnoreCase("SIM_AES") == 0) {
      parent = "SIM_IPP";
    }
    if (source.compareToIgnoreCase("SIM_HONOLULU") == 0) {
      parent = "SIM_OAHU_GRID";
    }
    if (source.matches("SIM_HONOLULU_\\w*")) {
      parent = "SIM_HONOLULU";
    }
    if (source.compareToIgnoreCase("SIM_HPOWER") == 0) {
      parent = "SIM_IPP";
    }
    if (source.compareToIgnoreCase("SIM_KAHE") == 0) {
      parent = "SIM_OAHU_GRID";
    }
    if (source.matches("SIM_KAHE_\\w*")) {
      parent = "SIM_KAHE";
    }
    if (source.compareToIgnoreCase("SIM_KALAELOA") == 0) {
      parent = "SIM_IPP";
    }
    if (source.compareToIgnoreCase("SIM_WAIAU") == 0) {
      parent = "SIM_OAHU_GRID";
    }
    if (source.matches("SIM_WAIAU_\\w*")) {
      parent = "SIM_WAIAU";
    }
    return parent;
  }
}
